# -*- coding: utf-8 -*-
from PhysicObject import *
import random
#from spikes import *
#from enemyball import *
#from changeblock import *
#from speedblock import *
#from goal import *
TILE_SIZE = 10
DISPLAY_SIZE = (800, 600)

class Ball(PhysicObject):
#    image = load_image('images/ball_pink.png').convert_alpha()
    images = load_image([['images/ball violeta.png','images/enemy_ball_blue_B.png',
                        'images/ball roja.png','images/ball verde.png',
                        'images/ball violeta.png','images/enemy_ball_blue_B.png',
                        'images/ball roja.png','images/ball verde.png'],
                        ['images/ball_pink.png','images/ball_purple.png',
                        'images/ball_blue.png','images/ball_gray.png',
                        'images/ball_green.png','images/ball_yellow.png',
                        'images/ball_red.png','images/ball_gray.png']])

    #image_s = load_image(['images/ball violeta.png','images/ball base.png',
    #                    'images/ball roja.png','images/ball verde.png'])
   
    def __init__(self, world, x, y, dinamyc=1, restitucion=0.8):
        self.dinamyc_l = dinamyc
        self.world = world
        bodyDef = box2d.b2BodyDef()
        bodyDef.position = (x, y)
        bodyDef.userData = self
        bodyDef.isBullet = True
        self.body = self.world.CreateBody(bodyDef)
        shapeDef = box2d.b2CircleDef()
        shapeDef.radius = 2.3
        shapeDef.density = 1
        shapeDef.friction = 1.05
        shapeDef.restitution = restitucion
        if not dinamyc:
            shapeDef.radius = 4.6
            shapeDef.restitution = 1.5
            shapeDef.friction = 0.95
        self.body.AllowSleeping(True)
        self.shape=self.body.CreateShape(shapeDef)

       
        index=random.randint(0, 7)

        if dinamyc:
            self.body.SetMassFromShapes()
        

        self.image = self.images[dinamyc][index]

       
                
    def draw( self, surface, offset_x=0, offset_y=0 ):

        offset_x = 25 
        offset_y = 25
        if not self.dinamyc_l:
            offset_x = 45 
            offset_y = 50

        (screen_width,screen_height) = DISPLAY_SIZE
        pos = self.body.GetPosition()

        rotated = pygame.transform.rotate(self.image, -self.body.angle*180/3.14)
        
        rotation_offset_x = (rotated.get_width() - self.image.get_width())/2
        rotation_offset_y = (rotated.get_height() - self.image.get_height())/2

        surface.blit(rotated, (int(pos.x*TILE_SIZE)-offset_x-rotation_offset_x,
                     int(pos.y*TILE_SIZE)-offset_y-rotation_offset_y))


    def sleeping( self):
        return self.body.IsSleeping()

    def isAllowSleep(self):
        """Indica si tiene permitido dormirse el objeto"""
        pass

    def destroy( self):
        """Destruye el objeto """
        self.body.DestroyShape (self.shape)  
